package me.ingxin.android.keyboard.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import me.ingxin.android.keyboard.entry.Key

/**
 * Created by ingxin on 2023/2/6
 * 字母键盘,字母部分
 */
open class LetterKeyboardView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : BaseKeyboardView(context, attrs) {

    /**
     * 0-9:数字
     * 10-35:字母
     * 36:shift
     * 37:空格
     * 38:删除
     */
    private var keyShiftIndex = -1

    /**小写字母列表*/
    private val letters = charArrayOf(
        'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
        'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
        'z', 'x', 'c', 'v', 'b', 'n', 'm'
    )

    /**
     * 创建键盘布局
     * 0-9:数字
     * 10-35:字母
     * 36:shift
     * 37:空格
     * 38:删除
     */
    override fun onCreateKeyboard(suggestKeyWidth: Float, suggestKeyHeight: Float): List<Key> {
        val keys = ArrayList<Key>()

        //row0:数字1-0
        var row0X = paddingStart.toFloat()
        val row0Y = paddingTop.toFloat()
        for (i in 1..10) {
            val num = if (i == 10) {
                '0'
            } else {
                '0' + i
            }
            keys.add(
                Key(
                    row0X, row0Y, row0X + suggestKeyWidth, row0Y + suggestKeyHeight,
                    num.code, num.toString()
                )
            )
            row0X += suggestKeyWidth + horizontalGap
        }

        //row1:q-p
        var row1X = paddingStart.toFloat()
        val row1Y = paddingTop + suggestKeyHeight + verticalGap
        for (i in 0 until 10) {
            val letter = letters[i]
            keys.add(
                Key(
                    row1X, row1Y, row1X + suggestKeyWidth, row1Y + suggestKeyHeight,
                    letter.code, letter.toString()
                )
            )
            row1X += suggestKeyWidth + horizontalGap
        }

        //row2:a-l
        var row2X = paddingStart.toFloat() + suggestKeyWidth + horizontalGap
        val row2Y = paddingTop + (suggestKeyHeight + verticalGap) * 2
        for (i in 10 until 19) {
            val letter = letters[i]
            keys.add(
                Key(
                    row2X, row2Y, row2X + suggestKeyWidth, row2Y + suggestKeyHeight,
                    letter.code, letter.toString()
                )
            )
            row2X += suggestKeyWidth + horizontalGap
        }

        //row3:z-m
        val useWidth = suggestKeyWidth * 7 + horizontalGap * 6
        var row3X = (width - useWidth) / 2f
        val tempRow3X = row3X
        val row3Y = paddingTop + (suggestKeyHeight + verticalGap) * 3
        for (i in 19 until 26) {
            val letter = letters[i]
            keys.add(
                Key(
                    row3X, row3Y, row3X + suggestKeyWidth, row3Y + suggestKeyHeight,
                    letter.code, letter.toString()
                )
            )
            row3X += suggestKeyWidth + horizontalGap
        }

        //shift键
        keyShiftIndex = 36
        keys.add(
            Key(
                paddingStart.toFloat(), row2Y, suggestKeyWidth,
                row2Y + suggestKeyHeight, Key.CODE_SHIFT_UP, "shift"
            )
        )

        //空格
        keys.add(
            Key(
                paddingStart.toFloat(), row3Y, tempRow3X - horizontalGap,
                row3Y + suggestKeyHeight, ' '.code, "space"
            )
        )

        //删除键
        keys.add(
            Key(
                row3X + horizontalGap, row3Y, (width - paddingEnd).toFloat(),
                row3Y + suggestKeyHeight, Key.CODE_DELETE, "delete"
            )
        )
        return keys
    }

    override fun drawKeyBackground(canvas: Canvas, key: Key, background: Drawable?) {
        if (key.code == Key.CODE_SHIFT_LOWER && background != null) {
            //选中状态
            background.state = intArrayOf(android.R.attr.state_selected)
            background.setBounds(
                key.left.toInt(), key.top.toInt(), key.right.toInt(), key.bottom.toInt()
            )
            background.draw(canvas)
        } else {
            super.drawKeyBackground(canvas, key, background)
        }
    }

    override fun getMaxRowSize(): Int = 4

    override fun getMaxColumnSize(): Int = 10

    override fun onConfirmKey(key: Key) {
        super.onConfirmKey(key)
        when (key.code) {
            Key.CODE_SHIFT_UP -> toUpperCase()
            Key.CODE_SHIFT_LOWER -> toLowerCase()
        }
    }

    private fun toUpperCase() {
        val keys = keyBuffer ?: return
        for (i in 10..35) {
            val key = keys[i]
            //转大写
            key.code = key.code - 32
            key.label = Char(key.code).toString()
        }
        if (keyShiftIndex in keys.indices) {
            keys[keyShiftIndex].let {
                it.code = Key.CODE_SHIFT_LOWER
                it.label = "shift"
            }
        }
        invalidate()
    }

    private fun toLowerCase() {
        val keys = keyBuffer ?: return
        for (i in 10..35) {
            val key = keys[i]
            //转小写
            key.code = key.code + 32
            key.label = Char(key.code).toString()
        }
        if (keyShiftIndex in keys.indices) {
            keys[keyShiftIndex].let {
                it.code = Key.CODE_SHIFT_UP
                it.label = "shift"
            }
        }
        invalidate()
    }

    override fun resetUI() {
        super.resetUI()
        val keys = keyBuffer ?: return
        if (keyShiftIndex in keys.indices) {
            if (keys[keyShiftIndex].code != Key.CODE_SHIFT_UP) {
                toLowerCase()
            }
        }
    }


}